
registerOutputEvent("fxDTSBrick", "z_fakeEvent_addRandomItem", "int 0 9 0", 0);

datablock fxDTSBrickData(BrickRandomItemSpawnData : Brick2x2fData) {
	category = "Special";
	subCategory = "Interactive";
	uiName = "Random Item Spawn";
};

$AddRandomItem::Rarity0 = "<color:969696>Ubiquitous";
$AddRandomItem::Rarity1 = "<color:D6D6Dy>Familiar";
$AddRandomItem::Rarity2 = "<color:F5F5F5>Common";
$AddRandomItem::Rarity3 = "<color:FFFC99>Usual";
$AddRandomItem::Rarity4 = "<color:FFFF00>Unusual";
$AddRandomItem::Rarity5 = "<color:824100>Uncommon";
$AddRandomItem::Rarity6 = "<color:EB7300>Rare";
$AddRandomItem::Rarity7 = "<color:00FFFF>Very Rare";
$AddRandomItem::Rarity8 = "<color:A5FFFF>Epic";
$AddRandomItem::Rarity9 = "<color:DC19D7>Legendary";

$AddRandomItem::ShapeNameDist = 25;

exec("./support.cs");
luaexec("Add-ons/Event_AddRandomItem2/rarity.lua");

function AddRandomItem_getItemDirection(%dir) {
	%dir = mClamp(%dir, 2, 5);
	%obj.itemDirection = %dir;
	if (!isObject(%obj.Item))
	{
		return;
	}
	%pos = getWords(%obj.Item.getTransform(), 0, 2);
	if      (%dir == 2) %rot = "0 0 1 0";
	else if (%dir == 3) %rot = "0 0 1 " @ $piOver2;
	else if (%dir == 4) %rot = "0 0 -1 " @ $pi;
	else if (%dir == 5) %rot = "0 0 -1 " @ $piOver2;
	else                %rot = "0 0 1 0";
	return %rot;
}

registerOutputEvent("fxDTSBrick", "setRandomItem", "paintColor 0", 0);
function fxDTSBrick::setRandomItem(%brick, %colorId) {
	%item = getRandomItemColorBased(%colorId);
	if(!isObject(%item)) return;
	%brick.setItem(%item);
}

registerOutputEvent("fxDTSBrick", "spawnRandomItem", "vector 200" TAB "paintColor 0" TAB "vector 200", 0);
function fxDTSBrick::spawnRandomItem(%brick, %vel, %colorId, %variance) {
	%item = getRandomItemColorBased(%colorId);
	if(!isObject(%item)) return;
	%var2 =
		((getRandom() - 0.5) * getWord(%variance, 0)) SPC
		((getRandom() - 0.5) * getWord(%variance, 1)) SPC
		((getRandom() - 0.5) * getWord(%variance, 2))     ;
	%vel = vectorAdd(%vel, %var2);
	%brick.spawnItem(%vel, %item, %variance);
}

registerOutputEvent("Player", "addRandomItem", "paintColor 0", 0);
function Player::addRandomItem(%player, %colorId) {
	%item = getRandomItemColorBased(%colorId);
	if(!isObject(%item)) return;
	%player.addItem(%item, %player.client);
}

registerOutputEvent("Bot", "dropRandomItem", "paintColor 0" TAB "float 0 100 5 100", 0);
function AIPlayer::dropRandomItem(%bot, %colorId, %chance) {
	%item = getRandomItemColorBased(%colorId);
	if(!isObject(%item)) return;
	%bot.dropItem(%item, %chance);
}

registerOutputEvent("Bot", "setRandomItem", "paintColor 0", 0);
function AIPlayer::setRandomItem(%bot, %colorId) {
	%item = getRandomItemColorBased(%colorId);
	if(!isObject(%item)) return;
	%bot.setWeapon(%item);
}

function getRandomItemColorBased(%colorId) {
	return luacall("AddRandomItem_getRandomItemColorBased", %colorId);
}

function fxDTSBrick::addRandomItem_onLoadPlanted(%brick) {
	if(!isObject(%brick.item))
		return;
	
	for(%i = 0; %i < %brick.numEvents; %i++) {
		if(%brick.eventOutput[%i] $= "z_fakeEvent_addRandomItem") {
			%raritySlot = mClampF(mFloor(%brick.eventOutputParameter[%i, 1]), 0, 9);
			break;
		}
	}
	
	if(%raritySlot $= "")
		return;
	
	%rarity = $AddRandomItem::Rarity[%raritySlot];
	%color = addRandomItem_hexToRGB(getSubStr(%rarity, 7, strLen(%rarity) - strLen(stripMLControlChars(%rarity)) - 8));
	
	%brick.item.setShapeName("Rarity:" SPC stripMLControlChars(%rarity) @ "(" @ %raritySlot @ ")");
	%brick.item.setShapeNameColor(%color);
	%brick.item.setShapeNameDistance($AddRandomItem::ShapeNameDist);
	
	%brick.addRandomItem_rarity = %raritySlot;
}

function serverCmdSetRandItemColorName(%client, %word1, %word2, %word3, %word4, %word5, %word6, %word7, %word8, %word9, %word10, %word11) {
	if(!%client.isAdmin) return;
	
	%colorID = mClampF(mFloor(%client.currentColor), 0, 64);
	%name = trim(%word1 SPC %word2 SPC %word3 SPC %word4 SPC %word5 SPC %word6 SPC %word7 SPC %word8 SPC %word9 SPC %word10 SPC %word11);
	%hex = addRandomItem_rgbToHex(getColorIDTable(%client.currentColor));
	
	if(strLen(%name) > 64) %name = getSubStr(%name, 0, 64);
	
	$AddRandomItem::ColorName[%colorID] = %name;
	
	commandToClient(%client, 'centerPrint', "\c6Set random item category color ID<color:" @ %hex @ ">" SPC %client.currentColor SPC "\c6to<color:" @ %hex @ ">" SPC %name @ "\c6.", 5);
}

package Event_AddRandomItem {
	function fxDTSBrickData::onTrustCheckFinished(%data, %brick) {
		if(isObject(%brick.client))
			%client = %brick.client;
		else if(isObject(%brick.getGroup().client))
			%client = %brick.getGroup().client;
		else if(isObject(findClientByBL_ID(%brick.getGroup().bl_id)))
			%client = findClientByBL_ID(%brick.getGroup().bl_id);
		
		if(!isObject(%client) || !isObject(%player = %client.player)) {
			if(%data.getName() $= "BrickRandomItemSpawnData" && !%client.isAdmin) {
				%failed = true;
				brickPlantSound.setName("BrickPlantSound_Temp");
			}
		}
		
		parent::onTrustCheckFinished(%data, %brick);
		
		if(%failed) {
			BrickPlantSound_Temp.setName("BrickPlantSound");
			
			commandToClient(%client, 'centerPrint', "You must be an \c6Admin\c0 to plant this brick.", 2);
			%brick.trustCheckFailed();
		}
	}
	
	function fxDTSBrick::setColor(%brick, %colorId) {
		%oldColorId = %brick.getColorId();
		parent::setColor(%brick, %colorId);
		%newColorId = %brick.getColorId();
		
		if(%brick.getDataBlock().getName() $= "BrickRandomItemSpawnData" && isObject(%brick.item)) {
			luacall("AddRandomItem_RemoveBrick", %oldColorId, %brick);
			luacall("AddRandomItem_AddBrick"   , %newColorId, %brick);
		}
	}

	function fxDTSBrick::setItem(%brick, %item) {
		%prevItem = isObject(%brick.item) ? %brick.item.getDataBlock() : 0;
		
		parent::setItem(%brick, %item);
		
		if(%brick.getDataBlock().getName() $= "BrickRandomItemSpawnData") {
			if(isObject(%item)) {
				%colorId = %brick.getColorId();
				if(!isObject(%prevItem))
					luacall("AddRandomItem_AddBrick", %colorId, %brick);
				
				%brick.item.canPickUp = false;
				
				%slot = mClampF(%brick.addRandomItem_rarity, 0, 9);
				%rarity = $AddRandomItem::Rarity[%slot];
				%color = addRandomItem_hexToRGB(getSubStr(%rarity, 7, strLen(%rarity) - strLen(stripMLControlChars(%rarity)) - 8));
				
				%brick.item.setShapeName("Rarity:" SPC stripMLControlChars(%rarity) @ "(" @ %slot @ ")");
				%brick.item.setShapeNameColor(%color);
				%brick.item.setShapeNameDistance($AddRandomItem::ShapeNameDist);
			}
			else if(isObject(%prevItem))
				luacall("AddRandomItem_RemoveBrick", %colorId, %brick);
			
			%count = clientGroup.getCount();
			
			for(%i = 0; %i < %count; %i++){
				if(isObject(%player = clientGroup.getObject(%i).player) && isObject(%player.isSettingRandomItemRarity) && %player.isSettingRandomItemRarity == %brick) {
					%player.isSettingRandomItemRarity = 0;
					commandToClient(clientGroup.getObject(%i), 'clearCenterPrint');
				}
			}
		}
	}

	function fxDTSBrick::onActivate(%brick, %player, %client, %pos, %vec) {
		parent::onActivate(%brick, %player, %client, %pos, %vec);
		
		%data = %brick.getDataBlock();
		
		if(%data.getName() !$= "BrickRandomItemSpawnData" || !isObject(%brick.item) || !%client.isAdmin)
			return;
		
		%player.isSettingRandomItemRarity = %brick;
		
		commandToClient(%client, 'centerPrint', "\c6Press \c30\c6 - \c39\c6 to set the random item rarity for<color:" @ addRandomItem_rgbToHex(getColorIDTable(%brick.getColorID())) @ ">" SPC %brick.item.getDataBlock().uiName @ "\c6.", 0);
	}
	
	function serverCmdUseInventory(%client, %slot) {
		if(isObject(%player = %client.player) && isObject(%brick = %player.isSettingRandomItemRarity)) {
			%slot = %slot >= 9 ? 0 : %slot + 1;
			%rarity = $AddRandomItem::Rarity[%slot];
			
			%player.currSelectedRandomItemRarity = %slot;
			
			commandToClient(%client, 'centerPrint', "\c6Rarity:" SPC %rarity @ "(" @ %slot @ ")\c6.\n\c6Press \c2Plant Brick\c6 to confirm. Press \c0Cancel Brick\c6 to cancel.", 0);
			
			return;
		}
		
		parent::serverCmdUseInventory(%client, %slot);
	}
	
	function serverCmdPlantBrick(%client) {
		if(isObject(%player = %client.player) && isObject(%brick = %player.isSettingRandomItemRarity)) {
			%slot = mClampF(mFloor(%player.currSelectedRandomItemRarity), 0, 9);
			%rarity = $AddRandomItem::Rarity[%slot];
			%color = addRandomItem_hexToRGB(getSubStr(%rarity, 7, strLen(%rarity) - strLen(stripMLControlChars(%rarity)) - 8));
			
			commandToClient(%client, 'centerPrint', "\c3" @ %brick.item.getDataBlock().uiName SPC "\c6rarity set to" SPC %rarity @ "(" @ %slot @ ")", 3);
			
			%player.isSettingRandomItemRarity = 0;
			%player.currSelectedRandomItemRarity = 0;
			
			%brick.item.setShapeName("Rarity:" SPC stripMLControlChars(%rarity) @ "(" @ %slot @ ")");
			%brick.item.setShapeNameColor(%color);
			%brick.item.setShapeNameDistance($AddRandomItem::ShapeNameDist);
			
			%brick.addRandomItem_rarity = %slot;
			
			for(%i = 0; %i < %brick.numEvents; %i++) {
				if(%brick.eventOutput[%i] $= "z_fakeEvent_addRandomItem") {
					%brick.eventOutputParameter[%i, 1] = %slot;
					%foundEvent = true;
					break;
				}
			}
			
			if(!%foundEvent)
				%brick.addEvent(1, 0, "onActivate", "Self", "z_fakeEvent_addRandomItem", %slot);
			
			return;
		}
		
		parent::serverCmdPlantBrick(%client);
	}
	
	function serverCmdCancelBrick(%client) {
		if(isObject(%player = %client.player) && isObject(%brick = %player.isSettingRandomItemRarity)) {
			%player.isSettingRandomItemRarity = 0;
			%player.currSelectedRandomItemRarity = 0;
			
			commandToClient(%client, 'clearCenterPrint');
			
			return;
		}
		
		parent::serverCmdCancelBrick(%client);
	}
	
	function fxDTSBrick::onLoadPlant(%brick) {
		parent::onLoadPlant(%brick);
		%brick.schedule(1000, addRandomItem_onLoadPlanted);
	}
};
activatePackage(Event_AddRandomItem);

function fxDtsBrick::z_fakeEvent_addRandomItem() {}

function arir() {
	exec("./server.cs");
}
